package com.scau.yangsuiyu.estore.controller.buyer;

import com.alibaba.fastjson.JSONObject;
import com.scau.yangsuiyu.estore.controller.myComponent.ConfirmOrderHBox;
import com.scau.yangsuiyu.estore.dababases.entity.Buyer;
import com.scau.yangsuiyu.estore.dababases.entity.Item;
import com.scau.yangsuiyu.estore.dababases.entity.Orders;
import com.scau.yangsuiyu.estore.service.OrderService;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.control.Pagination;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @author suiyu_yang
 * @description 确认订单界面
 * @date 2019-06-13 15:10
 * @email suiyu_yang@163.com
 */
public class ConfirmOrderPane extends AnchorPane implements Initializable {
    private Stage stage;
    private Buyer buyer;
    private Orders orders = new Orders();
    private List<Item> currentItemsList;
    private int itemsPerPage = 3;
    private int sumPaymentPrice = 0;
    private int sumTotalPrice = 0;
    private int sumAmount = 0;
    @FXML
    private Pagination pagination;

    @FXML
    private Label amountOfAllItemLabel;

    @FXML
    private VBox itemsListVBox;

    @FXML
    private Label sumPriceOfAllItemLabel;

    @FXML
    private Label address;


    public ConfirmOrderPane(Buyer buyer, List<Item> itemsList) {
        this.buyer = buyer;
        this.currentItemsList = itemsList;
        loadFXML();
    }

    private void loadFXML() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/buyer/ConfirmOrderPane.fxml"));
        fxmlLoader.setController(this);
        fxmlLoader.setRoot(this);
        try {
            fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        setAllItemsToUI();
        updateOtherUI();
        updatePages();
    }

    public void showPane() {
        stage = new Stage();
        stage.setScene(new Scene(this));
        stage.show();
    }

    @FXML
    private void submitOrder() {
        JSONObject resultJson = new OrderService(getOrderFromUI()).createOrder(currentItemsList);
        new Alert(Alert.AlertType.INFORMATION, resultJson.getString("result")).showAndWait();
        stage.close();
    }

    private Orders getOrderFromUI() {

        orders.setBuyer(buyer);
        orders.setPaymentPrice(sumPaymentPrice);
        orders.setTotalPrice(sumTotalPrice);
        orders.setAddress(address.getText());

        return orders;
    }

    private void setAllItemsToUI() {
        for (Item item : currentItemsList) {
            sumPaymentPrice += item.getPaymentPrice();
            sumTotalPrice += item.getTotalPrice();
            sumAmount += item.getAmount();
        }
    }

    private void updateOtherUI() {
        address.setText(buyer.getDefaultDeliveryAddress().toString());
        sumPriceOfAllItemLabel.setText("¥" + sumPaymentPrice);
        amountOfAllItemLabel.setText(sumAmount + "");
    }

    /**
     * 更新页面
     */
    private void updatePages() {
        itemsListVBox.getChildren().clear();

        int pageNum;
        if (currentItemsList.size() % itemsPerPage == 0) {
            pageNum = currentItemsList.size() / itemsPerPage;
        } else {
            pageNum = currentItemsList.size() / itemsPerPage + 1;
        }
        pagination.setPageCount(pageNum);
        pagination.setPageFactory((Integer pageIndex) -> createPage(pageIndex));
    }

    /**
     * 创建每一页的对象
     *
     * @param pageIndex
     * @return
     */
    private VBox createPage(int pageIndex) {
        int page = pageIndex * itemsPerPage; //当前是第几个商品
        if (currentItemsList.size() != 0) {
//            itemsListVBox.getChildren().clear();
            for (int i = page; i < page + itemsPerPage; i++) {
                if (i < currentItemsList.size()) {
                    ConfirmOrderHBox confirmOrderHBox = new ConfirmOrderHBox(currentItemsList.get(i));
                    itemsListVBox.getChildren().add(confirmOrderHBox);
                }
            }
        }
        return itemsListVBox;
    }

}
